﻿using Catastrophe.BasePC.UI.MyEnum;
using Catastrophe.BasePC.UI.SystemIni.BaseData;
using Catastrophe.BasePC.UI.DbDal;
using Catastrophe.BasePC.UI.Model;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Catastrophe.BasePC.UI.UIMode
{
    /// <summary>
    /// 用户编辑
    /// </summary>
    public partial class UcSysUserEditModel : ObservableObject
    {
        /// <summary>
        /// 部门
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<SysDictionaryEntity> dicDepartment;
        /// <summary>
        /// 选中的部门
        /// </summary>
        [ObservableProperty]
        private SysDictionaryEntity selectDicDepartment;
        /// <summary>
        /// 身份
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<SysDictionaryEntity> dicRole;
        /// <summary>
        /// 选中的身份
        /// </summary>
        [ObservableProperty]
        private SysDictionaryEntity selectDicRole;

        [ObservableProperty]
        private string userName;

        [ObservableProperty]
        private string password;
        [ObservableProperty]
        private string phone;
        /// <summary>
        /// 备注
        /// </summary>
        [ObservableProperty]
        private string remake;

        /// <summary>
        /// 窗口名称
        /// </summary>
        [ObservableProperty]
        private string windowTitleName;
        /// <summary>
        /// 
        /// </summary>
        public SysUserEntity selectDataList {  get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int dataId { get; set; }
        /// <summary>
        /// 词典
        /// </summary>
        SysDictionaryDal dictionaryDal { get; set; }
        /// <summary>
        /// 用户
        /// </summary>
        SysUserDal userDal { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public WindowTypeEnum windowType { get; set; }
        public bool Flag {  get; set; }
        /// <summary>
        /// 
        /// </summary>
        public event Action<bool> FlagChanged;
        /// <summary>
        /// 
        /// </summary>
        public ICommand SaveCommand { get; set; }
        public UcSysUserEditModel()
        {
            dictionaryDal = new SysDictionaryDal();
            userDal=new SysUserDal();
            dicDepartment = new ObservableCollection<SysDictionaryEntity>();
            dicRole = new ObservableCollection<SysDictionaryEntity>();
            SaveCommand = new RelayCommand(SaveCommandMethod);
            LoadData();
        }

        private void SaveCommandMethod()
        {
            SysUserEntity sysUserEntity = new SysUserEntity()
            {
                UserName = UserName,
                LoginPassword = Password,
                TimeStamp=DateTime.Now.ToString(),
                Phone=Phone,
                Remark=Remake,
                Id=dataId,
            };
            if (SelectDicRole != null)
            {
                sysUserEntity.Identify = SelectDicRole.DicName;
            }
            if (SelectDicDepartment!=null)
            {
                sysUserEntity.AuthorizeType = SelectDicDepartment.DicName;
            }
            if (windowType == WindowTypeEnum.Add)
            {
                userDal.AddEntitie(sysUserEntity);
                Flag = true;
                FlagChanged?.Invoke(Flag);
            }
            if (windowType == WindowTypeEnum.Edit)
            {
                userDal.Update(sysUserEntity);
                Flag = true;
                FlagChanged?.Invoke(Flag);
            }
        }

        private void LoadData()
        {
            Task.Factory.StartNew(() =>
            {
                var data = dictionaryDal.QueryByKey(DataIniEntity.dic_Type_部门);
                data.ForEach(d =>
                {
                    dicDepartment.Add(d);
                });
                if (DicDepartment.Count>0)
                {
                    SelectDicDepartment = DicDepartment[0];
                }
                data = dictionaryDal.QueryByKey(DataIniEntity.dic_Type_身份);
                data.ForEach(d =>
                {
                    dicRole.Add(d);
                });
                if (DicRole.Count>0)
                {
                    SelectDicRole = DicRole[0];
                }
            });

        }

        public void SetSelectItem()
        {
            Phone = selectDataList.Phone;
            UserName = selectDataList.UserName;
            Password = selectDataList.LoginPassword;
            Remake = selectDataList.Remark;
            dataId= selectDataList.Id;
            foreach (var d in DicDepartment)
            {
                if (d.DicName== selectDataList.AuthorizeType)
                {
                    SelectDicDepartment = d;
                    break;
                }
            }
            foreach (var d in DicRole)
            {
                if (d.DicName == selectDataList.Identify)
                {
                    SelectDicRole= d;
                    break;
                }
            }
        }
    }
}

